import Ele from './Ele'
import IfParser from '../parser/IfParser'
import Okay  from '../Okay'
export default class IfEle extends Ele {
    constructor(subject, ele) {
        super(subject, ele)
        this.if_ele = document.createElement('if-item')
        this.else_ele = document.createElement('else-item')
        let childnodes = this.ele.childNodes
        while(childnodes.length > 0) {
            let child = childnodes[0]
            if(child.tagName == 'ELSE') {
                this.else_ele.appendChild(child)
            }else {
                this.if_ele.appendChild(child)
            }
        }
        this.okays = []
        new Okay({
            ele: this.if_ele,
            data: this.subject.data,
            builder: this.subject.builder,
            filters: this.filters,
            parent: this,
            aop: this.subject.aop || {},
            action_classes: this.subject.action_classes
        })
        new Okay({
            ele: this.else_ele,
            data: this.subject.data,
            builder: this.subject.builder,
            filters: this.subject.filters,
            parent: this,
            aop: this.subject.aop || {},
            action_classes: this.subject.action_classes
        })
    }
    getView () {
        return this.ele.getAttribute('condition')
    }
    getParser () {
        return new IfParser(this.getView())
    }
    draw () {
        super.draw()
        if (this.fn.call(this.subject)) {
            this.ele.innerHTML = ''
            this.ele.appendChild(this.if_ele)
        }else {
            this.ele.innerHTML = ''
            if (this.else_ele) {
                this.ele.appendChild(this.else_ele)
            }
        }
    }
    update() {
        if (this.fn.call(this.subject)) {
            this.ele.innerHTML = ''
            this.ele.appendChild(this.if_ele)
        }else {
            this.ele.innerHTML = ''
            if (this.else_ele) {
                this.ele.appendChild(this.else_ele)
            }
        }
    }
}